#include<allegro.h>
#include <stdio.h>
#include "leo.h"
#include "bullet.h"

void load_bullet(Bullet *B)
{
    char buf[50];

    sprintf(buf, "Bullet_right.bmp");
    B->img_right = load_bitmap(buf, NULL);
    sprintf(buf, "Bullet_left.bmp");
    B->img_left = load_bitmap(buf, NULL);
}

void destroy_bullet(Bullet *B)
{
    destroy_bitmap(B->img_right);
    destroy_bitmap(B->img_left);
}

void init_bullet(Bullet *B, Leo leo)
{
    if (leo.direction == 0)
    {
        B->direction_b = 0;
        B->xb = leo.x+88;
        B->yb = leo.y+62;
    }
    if (leo.direction == 1)
    {
        B->direction_b = 1;
        B->xb = leo.x;
        B->yb = leo.y+62;
    }
}

void move_bullet_right(Bullet *B,int *compteur, Leo leo)
{
    if (key[KEY_SPACE] && leo.direction==0)
    {

        *compteur+=1;

        (B->xb)+=30;
        B->direction_b = 0;

    }
}

void move_bullet_left(Bullet *B,int *compteur,Leo leo)
{
    if (key[KEY_SPACE] && leo.direction==1)
    {

        *compteur+=1;

        (B->xb)-=30;
        B->direction_b = 1;
    }
}

void move_bullet(Bullet *B, int *compteur,Leo leo)
{
    move_bullet_right(B,compteur,leo);
    move_bullet_left(B,compteur,leo);
}

void draw_bullet(Bullet B,BITMAP *Buffer, int compteur)
{


    if (B.direction_b == 0)
    {

        draw_sprite(Buffer, B.img_right , B.xb, B.yb);

    }

    if(B.direction_b == 1)
    {

        draw_sprite(Buffer, B.img_left, B.xb, B.yb);

    }

}